package com.quanyan.platform.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.platform.client.IGatherTagService;
import com.quanyan.platform.model.req.ReqNearbyCircleInfo;
import com.quanyan.platform.model.req.ReqRecommendOrNearby;
import com.quanyan.platform.model.resp.RespGatherCircleRecords;
import com.quanyan.platform.model.resp.RespGatherItems;
import com.quanyan.platform.model.resp.RespGatherSportCategories;
import com.quanyan.platform.model.resp.RespGatherSpots;
import com.quanyan.platform.response.RespGatherTags;
import com.quanyan.platform.service.GatherService;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by slong.guo on 2017/3/7.
 */

@RestController
@RequestMapping(value = "/api/platform/gather", method = RequestMethod.POST, produces = "application/json")
public class GatheringController {

    private static final Logger logger = LoggerFactory.getLogger(GatheringController.class);

    @Autowired
    private GatherService gatherService;

    @Autowired
    private IGatherTagService iGatherTagService;


    /**
     * 获取运动类型（足篮网羽）信息
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/sports")
    public APIResponse<RespGatherSportCategories> getGatherSpots(@RequestBody Map map, HttpServletRequest request) {
        return gatherService.getSports();
    }


    /**
     * 获取附近的圈子信息
     * @param reqCircle
     * @param request
     * @return
     * @throws TException
     */
    @RequestMapping(value = "/circles")
    public APIResponse<RespGatherSpots> getCircles(@RequestBody ReqNearbyCircleInfo reqCircle, HttpServletRequest request) throws TException {
        if (reqCircle == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer uid = (Integer) request.getAttribute("uid");
        uid = uid == null ? 0 : uid;

        return gatherService.getCircles(uid, reqCircle);
    }

    /**
     * 关注某个圈子(这个接口现在用不到)
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/follow")
    public APIResponse follow(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        if (uid == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer circleID = (Integer) map.get("circleID");
        if (circleID == null) {
            return APIResponse.returnFail(ApiResponseEnum.NEED_USER_LOGIN);
        }

        return gatherService.doFollow(uid, circleID);
    }


    /**
     * 圈子关注记录（这个接口现在用不到）
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "record")
    public APIResponse<RespGatherCircleRecords> record(@RequestBody Map map, HttpServletRequest request) {

        Integer uid = (Integer) request.getAttribute("uid");
        if (uid == null) {
            return APIResponse.returnFail(ApiResponseEnum.NEED_USER_LOGIN);
        }

        Integer type = (Integer) map.get("type");
        if (type == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return gatherService.getRecord(uid, type);
    }

    /**
     * 获取某圈子的推荐/附近的场馆、俱乐部、活动信息
     * @param info
     * @param request
     * @return
     */
    @RequestMapping(value = "/recommendOrNearby")
    public APIResponse<RespGatherItems> recommendOrNearby(@RequestBody ReqRecommendOrNearby info, HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        uid = uid == null ? 0 : uid;

        if (info == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return gatherService.getRecommendOrNearby(uid, info);
    }


    /**
     * 获取聚集地标签信息，这个接口为内部测试使用，用户中心其实通过dubbo来调用，不走http协议
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/tags")
    public APIResponse<RespGatherTags> getGatherTags(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        uid = uid == null ? 0 : uid;

        Integer otherUID = (Integer) map.get("otherUID");
        Integer type = (Integer) map.get("type");
        if (otherUID == null || type == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return iGatherTagService.getGatherTags(uid, otherUID, type);
    }

    /**
     * 编辑聚集地标签信息，这个接口为内部测试使用，用户中心其实通过dubbo来调用，不走http协议
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/editTags")
    @SuppressWarnings("unchecked")
    public APIResponse editGatherTags(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer) request.getAttribute("uid");
        uid = uid == null ? 0 : uid;

        List<Integer> ids = (List<Integer>) map.get("ids");
        if (ids == null || ids.isEmpty()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }


        return iGatherTagService.editGatherTags(uid, ids);
    }

}
